<?php
/**
 * This is the model class for table "{{module_comment}}".
 *
 * The followings are the available columns in table '{{module_comment}}':
 * @property integer $comment_id
 * @property string $content_type
 * @property integer $uid
 * @property integer $cat_id
 * @property integer $content_id
 * @property string $content
 * @property integer $status
 * @property integer $pid
 * @property string $ip
 * @property integer $createtime
 * @property integer $updatetime
 */
class ModuleCommentModel extends BaseModel
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return ModuleCommentModel the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{module_comment}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('comment_type, content, status, ip', 'required'),
			array('comment_id, uid, cat_id, content_id, status, pid, createtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('comment_type', 'length', 'max'=>10),
			array('content', 'length', 'max'=>255),
			array('ip', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('comment_id, comment_type, uid, cat_id, content_id, content, status, pid, ip, createtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}
    /**
	 * 获取菜单列表
	 *
	 * @param array $params
	 * @return array 数据数组和分页对象
	 */
	public function getList($params){
		$iPage = (isset($params['page']) && !empty($params['page'])) ? $params['page'] : 1;
		$iPagesize = (isset($params['pagesize']) && !empty($params['pagesize'])) ? $params['pagesize'] : Yii::app()->params->pageSize;

		$criteria = new CDbCriteria();
        $criteria->order = 'createtime DESC' ;//排序条件 		
		$iCcount = self::model()->count($criteria);
		$oPages = new CPagination($iCcount);
		$oPages->pageSize = $iPagesize;
		$oPages->setCurrentPage($iPage-1);
		$oPages->applyLimit($criteria);
		$oResult = self::model()->findAll($criteria);

		$aList = array();
		if(!empty($oResult)){
			foreach ($oResult as $iKey => $aVal){
				$aList[$iKey]['comment_id'] = $aVal->comment_id;
                $aList[$iKey]['comment_type'] = $aVal->comment_type;
                $aList[$iKey]['uid'] = $aVal->uid;
                $aList[$iKey]['cat_id'] = $aVal->cat_id;
                $aList[$iKey]['content_id'] = $aVal->content_id;
                $aList[$iKey]['content'] = $aVal->content;
                $aList[$iKey]['status'] = $aVal->status;
				$aList[$iKey]['pid'] = $aVal->pid;
                $aList[$iKey]['ip'] = $aVal->ip;
                $aList[$iKey]['createtime'] = $aVal->createtime;
                $aList[$iKey]['updatetime'] = $aVal->updatetime;
				//$list[$key]['tabletree'] = $this->getTreeTable($fidvalue."_".$value['area_id']);
			}
		}
		$listResult['list'] = $aList;
		$listResult['pages'] = $oPages;

		return $listResult;
	}
    /**
     * ModuleCommentModel::getOne() 获取一条评论数据
     * 
     * @param int $iCommentId 评论id
     * @return array 一条评论数据
     */
    public function getOne($iCommentId){
        $oModel = $this->findByPk($iCommentId);
        return $oModel->attributes;
    }
    /**
     * ModuleCommentModel::deleteData() 删除数据
     * 
     * @param mixed $iCommentId
     * @return
     */
    public function deleteData($iCommentId){
        //为保证前台使用，这里只删除当前评论id的数据
        $oResult = $this->findByPk($iCommentId);
        if( $oResult && $oResult->delete() ){
            return true;
        } else {
            return false;
        }
    }
    /**
     * ModuleCommentModel::saveData() 保持数据
     * 
     * @param mixed $attributes
     * @return void
     */
    public function saveData($attributes){
        
    }
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'comment_id' => 'ID',
			'comment_type' => '评论类型',
			'uid' => '评论人',
			'cat_id' => '评论栏目',
			'content_id' => '原内容',
			'content' => '评论内容',
			'status' => '审核',
			'pid' => '回复ID',
			'ip' => '评论者IP',
			'createtime' => '创建时间',
			'updatetime' => '更新时间',
            'isguest' => '是否允许游客评论',
            'ischeck' => '是否需要审核',
            'iscode' => '是否开启验证码'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('comment_id',$this->comment_id);
		$criteria->compare('content_type',$this->content_type,true);
		$criteria->compare('uid',$this->uid);
		$criteria->compare('cat_id',$this->cat_id);
		$criteria->compare('content_id',$this->content_id);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('pid',$this->pid);
		$criteria->compare('ip',$this->ip,true);
		$criteria->compare('createtime',$this->createtime);
		$criteria->compare('updatetime',$this->updatetime);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}